/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx.wxs.controller;

import cn.webrx.wxs.mapper.SupplierMapper;
import cn.webrx.wxs.vo.VoSupplier;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Project: wxshop - AdminSupplier
 * <p>Powered by webrx On 2021-04-20 17:55:42
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 15
 */
@Controller
@RequestMapping("/admin")
public class AdminSupplier {
    @Autowired
    private SupplierMapper smdao;

    @GetMapping("/supplier/lists")
    public String lists(Model m) {
        //m.addAttribute("sus",smdao.selectAll());
        return "admin/supplier/lists";
    }

    @GetMapping("/supplier/edit/{id}")
    public String edit(@PathVariable("id") int id, Model m) {
        m.addAttribute("id", id);
        VoSupplier vo = smdao.queryById(id);
        m.addAttribute("su", vo);
        return "admin/supplier/edit";
    }

    @GetMapping("/supplier/add")
    public String add() {
        return "admin/supplier/add";
    }

    @PostMapping("/supplier/{id}")
    @ResponseBody
    public Map asave(@PathVariable("id") int id) {
        var map = new HashMap<String, Object>();
        if (smdao.delById(id) == 1) {
            map.put("msg", "删除成功");
            map.put("code", 1); //1 代表成功 0失败
        } else {
            map.put("msg", "删除失败");
            map.put("code", 0); //1 代表成功 0失败
        }
        return map;
    }

    @PostMapping("/supplier/esave")
    @ResponseBody
    public Map asave(int id, VoSupplier vs) {
        Map<String, Object> m = new HashMap<String, Object>();
        m.put("id", id);
        m.put("name", vs.getName());
        m.put("tel", vs.getTel());
        m.put("contact", vs.getContact());
        m.put("address", vs.getAddress());
        m.put("remark", vs.getRemark());

        var map = new HashMap<String, Object>();
        if (smdao.update(m) == 1) {
            map.put("msg", "修改完成");
            map.put("code", 1); //1 代表成功 0失败
        } else {
            map.put("msg", "修改失败");
            map.put("code", 0); //1 代表成功 0失败
        }
        return map;
    }

    @PostMapping("/supplier/asave")
    @ResponseBody
    public Map asave(VoSupplier vs) {
        var map = new HashMap<String, Object>();
        if (smdao.save(vs) == 1) {
            map.put("msg", "保存完成");
            map.put("code", 1); //1 代表成功 0失败
        } else {
            map.put("msg", "保存失败");
            map.put("code", 0); //1 代表成功 0失败
        }
        return map;
    }

    @GetMapping("/supplier/index")
    @ResponseBody
    public Map<String, Object> index(int page, int limit, @RequestParam(name = "keyword", required = false) String keyword) {
        Map<String, Object> map = new HashMap<String, Object>();
        PageHelper.startPage(page, limit);
        List data = smdao.selectAll(keyword == null ? null : "%" + keyword + "%");
        PageInfo pi = PageInfo.of(data);
        map.put("data", data);
        map.put("count", pi.getTotal());
        map.put("code", 1);
        map.put("msg", "");
        return map;
    }
}
